<!-- 新建供应商 -->
<template>
  <a-drawer v-model:visible="vdata.visible" :maskClosable="false" title="添加到内表" width="40%">
    <a-form
      ref="formRef"
      :model="vdata.saveObject"
      layout="vertical"
      labelAlign="left"
      :rules="vdata.rules"
      :colon="false"
      :wrapper-col="{ span: 24 }"
      style="margin-top: 30px; margin-left: 20px"
    >
      <a-row type="flex" justify="space-between" wrap="true">
        <a-col :span="11">
          <a-form-item label="商品名称" name="goodsName">
            <a-input v-model:value="vdata.saveObject['goodsName']" placeholder="供应商名称" />
          </a-form-item>
        </a-col>
        <!-- <a-col :push="1" :span="11">
          <a-form-item label="机构" name="agentNo">
            <GzTreeSelect ref="gzThreeRef" v-model:value="vdata.saveObject['agentNo']" />
          </a-form-item>
        </a-col> -->
        <a-col :span="11">
          <a-form-item label="市场价" name="marketPrice">
            <a-input v-model:value="vdata.saveObject['marketPrice']" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品详情" name="goodsDetail" :wrapper-col="{ span: 24 }">
            <a-textarea v-model:value="vdata.saveObject['goodsDetail']" :placeholder="vdata.saveObject['authConfigPlaceholder']" :rows="4" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商品图片" name="goodsImg" :wrapper-col="{span: 24}">
            <GzUpload v-model:src="vdata.saveObject['goodsImg']" bizType="activity" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="备注" name="remark" :wrapper-col="{ span: 24 }">
            <a-textarea v-model:value="vdata.saveObject['remark']" :placeholder="vdata.saveObject['authConfigPlaceholder']" :rows="4" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <div class="drawer-btn-center">
      <a-button :style="{ marginRight: '8px' }" style="margin-right: 8px" @click="onClose">
        <close-outlined />
        取消
      </a-button>
      <a-button type="primary" :loading="vdata.btnLoading" @click="handleOkFunc">
        <check-outlined />
        保存
      </a-button>
    </div>
  </a-drawer>
</template>
  
  <script setup lang="ts">
  // import { req,API_URL_VIRTUAGOODS_SUPPLIER_ADD,API_URL_VIRTUAL_GOODS_OUTSIDE_SAVE } from '@/api/manage'
  import {$apiAwardVirtualSupplierAdd,} from '@/api/externalGoods/supplier/index'
  import {$apiVirtualOutsideOutToInsideGoods} from '@/api/externalGoods/outsidetable/index'
  import { reactive, ref, defineExpose, getCurrentInstance } from 'vue'
  const { $infoBox, $access } = getCurrentInstance()!.appContext.config.globalProperties
  const formRef = ref()
  
  const props = defineProps(['callBackFunc'])
  
  const vdata:any = reactive({
    isAdd: false, // 新增OR修改
    visible: false,
    btnLoading: true,
    saveObject: {},
    rules: {
        goodsName: [{ required: true, message: '商品名称不能为空', trigger: 'blur' },],
        agentNo: [{ required: true, message: '机构不能为空', trigger: 'change' },],
        goodsImg: [{ required: true, message: '商品图片不能为空', trigger: 'change' },],
        marketPrice: [{ required: true, message: '市场价格不能为空', trigger: 'blur' },],
    },
  })
  
  function handleOkFunc() {
    formRef.value.validate().then(() => {
      vdata.btnLoading = true
      if(vdata.isAdd){  // 新增
        
        $apiAwardVirtualSupplierAdd(vdata.saveObject).then(res=>{
          $infoBox.message.success('新增成功！')
          props.callBackFunc()
          onClose()
        }).finally(()=>{
          vdata.btnLoading = false
        })
      }else{  // 修改
        // vdata.saveObject.marketPrice = vdata.saveObject.marketPrice*100
        
        $apiVirtualOutsideOutToInsideGoods({...vdata.saveObject,marketPrice:vdata.saveObject.marketPrice*100}).then(res=>{
          $infoBox.message.success('添加成功！')
          props.callBackFunc()
          onClose()
        }).finally(()=>{
          vdata.btnLoading = false
        })
      }
    }).catch(() => {
      vdata.btnLoading = false
    })
  }
  
  function onClose() {
    vdata.visible = false
  }
  function show(record) {    
    vdata.isAdd = !record
    vdata.btnLoading = false
    vdata.saveObject = {}
    if (formRef.value !== undefined) {
      formRef.value.resetFields()
    }
    if(!vdata.isAdd){ // 修改
      const mPrice = record.marketPrice*1 >=record.goodsPrice ? record.marketPrice*1 : record.goodsPrice
      vdata.saveObject = { ...record ,marketPrice: mPrice/100}
    }
    vdata.visible = true
  }
  
  defineExpose({
    show,
  })
  </script>